<?php
namespace app\index\controller;

use app\index\model\AccountModel;
use app\index\model\BidModel;
use app\index\model\DeptModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\QianBanListModel;
use app\index\model\QianBanModel;
use app\index\model\QianZhiGuanModel;
use app\index\model\QianPiaoListModel;
use app\index\model\QianPiaoModel;
use app\index\model\QianShouListModel;
use app\index\model\QianShouModel;
use app\index\model\QianZhiListModel;
use app\index\model\QianZhiModel;
use app\index\model\TouFanModel;
use app\index\model\TouHanModel;
use app\index\model\TouJiaoListModel;
use app\index\model\TouJiaoModel;
use app\index\model\UserModel;
use app\index\service\BidService;
use app\index\service\FinanceService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\QianService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Qian extends Common
{



    public function detail_qian_shou(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['shou.bidid','=', $param['bidid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|b.name|b.gofuer|list.explain|b.number|shou.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['list_explain']) && !empty($param['list_explain'])){
            $where[] = ['list.explain','like', '%'.$param['list_explain'].'%'];
        }
        if(isset($param['bid_number']) && !empty($param['bid_number'])){
            $where[] = ['b.number','like', '%'.$param['bid_number'].'%'];
        }
        if(isset($param['bid_gofuer']) && !empty($param['bid_gofuer'])){
            $where[] = ['b.gofuer','like', '%'.$param['bid_gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['shou.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['bid_state']) && !empty($param['bid_state'])){
            $where[] = ['b.state','=', $param['bid_state']];
        }
        if(isset($param['shouqianlei']) && !empty($param['shouqianlei'])){
            $where[] = ['list.shouqianlei','=', $param['shouqianlei']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        $data['shouqianlei'] = (new OptService())->get_base_info('cai_shouqianlei');
        $data['is_shouqianlei'] = isset($param['shouqianlei']) ? $param['shouqianlei'] : '';

        $data['bid_state'] = (new BidService())->get_bid_state();
        $data['is_bid_state'] = isset($param['bid_state']) ? $param['bid_state'] : '';

        $data['data'] = QianShouListModel::alias('list')
            ->field('shou.*,b.name as bidname,b.number as bidnumber,b.gofuer,b.state,bill.sericnum,
            list.shouqianlei,list.money as listmoney,list.explain as listexplain')
            ->join('qian_shou shou','shou.id=list.shouid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->leftJoin('bid b','b.id=shou.bidid')
            ->where($where)
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['shouqianlei_name'] = (new OptService())->get_base_showname('cai_shouqianlei', $item['shouqianlei']);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_qian_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['shou.bidid','=', $param['bidid']];
        }
        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['b.jinglei','=', $param['jinglei']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['b.name|shou.zhai|b.number|b.gofuer|shou.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['bid_number']) && !empty($param['bid_number'])){
            $where[] = ['b.number','like', '%'.$param['bid_number'].'%'];
        }
        if(isset($param['bid_gofuer']) && !empty($param['bid_gofuer'])){
            $where[] = ['b.gofuer','like', '%'.$param['bid_gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shou.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['shou.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['shou.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['bid_state']) && !empty($param['bid_state'])){
            $where[] = ['b.state','=', $param['bid_state']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }


        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['bid_state'] = (new BidService())->get_bid_state();
        $data['is_bid_state'] = isset($param['bid_state']) ? $param['bid_state'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['data'] = QianShouModel::alias('shou')
            ->field('shou.*,b.name as bidname,b.number as bidnumber,b.state as bidstate,b.status as bidstatus,b.jinglei
            ,gui_unit.name as gui_unit_name,b.gofuer,acc.name as accname,acc.accnum')
            ->join('bid b','b.id=shou.bidid')
            ->leftJoin('dept gui_unit','gui_unit.id=b.gui_unit')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('qian_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accid_name'] = $item['accname'].'('.$item['accnum'].')';
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen(['status'=>$item['bidstatus'],'state'=>$item['bidstate']]);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('qian_shou');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_qian_shou(Request $request){
        $param = $request->post();
        $data = QianShouModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            QianShouModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'qian_shou', $data['id'], '前期费收取-直营');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_qian_shou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = QianShouModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);

        $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid']);
        $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

        $list = QianShouListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['shouqianlei_name'] = (new OptService())->get_base_showname('cai_shouqianlei', $item['shouqianlei']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('qian_shou', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function detail_qian_ban(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['ban.deleted_user','=', 0];
        $where[] = ['ban.comid','=',$this->userinfo['comid']];
        $where[] = ['ban.isturn','=',1];
        $where[] = ['ban.status','in',[0,1]];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['ban.bidid','=', $param['bidid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|ban.gofuer|list.explain|ban.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['ban.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['list_explain']) && !empty($param['list_explain'])){
            $where[] = ['list.explain','like', '%'.$param['list_explain'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['ban.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['ban.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['ban.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['shouqianlei']) && !empty($param['shouqianlei'])){
            $where[] = ['list.shouqianlei','=', $param['shouqianlei']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['ban.status','=', $param['status']];
        }

        $data['shouqianlei'] = (new OptService())->get_base_info('cai_shouqianlei');
        $data['is_shouqianlei'] = isset($param['shouqianlei']) ? $param['shouqianlei'] : '';


        $data['data'] = QianBanListModel::alias('list')
            ->field('ban.*,bill.sericnum,
            list.shouqianlei,list.money as listmoney,list.explain as listexplain')
            ->join('qian_ban ban','ban.id=list.banid')
            ->leftJoin('flow_bill bill','bill.id=ban.billid')
            ->where($where)
            ->order('ban.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['shouqianlei_name'] = (new OptService())->get_base_showname('cai_shouqianlei', $item['shouqianlei']);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_qian_ban(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ban.deleted_user','=', 0];
        $where[] = ['ban.comid','=',$this->userinfo['comid']];
        $where[] = ['ban.isturn','=',1];
        $where[] = ['ban.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ban.zhai|b.number|ban.gofuer|ban.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['ban.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['ban.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['ban.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['ban.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['ban.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['ban.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['ban.status','=', $param['status']];
        }


        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $data['data'] = QianBanModel::alias('ban')
            ->field('ban.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=ban.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('qian_ban', 'ban'))
            ->order('ban.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accid_name'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('qian_ban');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_qian_ban(Request $request){
        $param = $request->post();
        $data = QianBanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            QianBanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'qian_ban', $data['id'], '前期费收取-办事处');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_qian_ban(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = QianBanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);

        $data['gofuerinfo'] = (new BidService())->get_dealcom_info($data['data']['gofuer']);
        $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

        $list = QianBanListModel::where(['banid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['shouqianlei_name'] = (new OptService())->get_base_showname('cai_shouqianlei', $item['shouqianlei']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('qian_ban', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }





    public function check_qian_zhi(Request $request){
        $str = "";
        $checkStatus = false;
        if($request->isPost()){
            $param = $request->post();
            $qian_count = (new QianService())->get_bid_qian_count($param['bidid']);
            if($param['money'] > $qian_count['ke_zhiyu_fee']){
                $checkStatus = true;
                $str .= "本次支付合计大于可支付金额【".$qian_count['ke_zhiyu_fee']."元】，是否继续提交？";
            }
        }
        return outputJson(2000,lang('code_2000'), ['status'=>$checkStatus, 'data'=>$str]);
    }
    public function detail_qian_zhi(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['zhi.bidid','=', $param['bidid']];
        }
        if(isset($param['listid']) && !empty($param['listid'])){
            $where[] = ['list.id','=', $param['listid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|b.name|b.gofuer|list.explain|list.fuer|b.number|zhi.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['list_explain']) && !empty($param['list_explain'])){
            $where[] = ['list.explain','like', '%'.$param['list_explain'].'%'];
        }
        if(isset($param['list_fuer']) && !empty($param['list_fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['list_fuer'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['zhi.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['bid_number']) && !empty($param['bid_number'])){
            $where[] = ['b.number','like', '%'.$param['bid_number'].'%'];
        }
        if(isset($param['bid_state']) && !empty($param['bid_state'])){
            $where[] = ['b.state','=', $param['bid_state']];
        }
        if(isset($param['bid_gofuer']) && !empty($param['bid_gofuer'])){
            $where[] = ['b.gofuer','like', '%'.$param['bid_gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['zhi.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['zhi.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['zhiqianlei']) && !empty($param['zhiqianlei'])){
            $where[] = ['list.zhiqianlei','=', $param['zhiqianlei']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }

        $data['zhiqianlei'] = (new OptService())->get_base_info('cai_zhiqianlei');
        $data['is_zhiqianlei'] = isset($param['zhiqianlei']) ? $param['zhiqianlei'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['bid_state'] = (new BidService())->get_bid_state();
        $data['is_bid_state'] = isset($param['bid_state']) ? $param['bid_state'] : '';

        $list = [];
        $info = QianZhiListModel::alias('list')
            ->field('zhi.*,b.name as bidname,b.number as bidnumber,b.gofuer,b.state,bill.sericnum,list.id as listid,
            list.zhiqianlei,list.money as listmoney,list.explain as listexplain,list.fuer,list.banknum,list.bankname,
            list.incdaotype,list.difee')
            ->join('qian_zhi zhi','zhi.id=list.zhiid')
            ->leftJoin('flow_bill bill','bill.id=zhi.billid')
            ->leftJoin('bid b','b.id=zhi.bidid')
            ->where($where)
            ->order('zhi.id DESC')
            ->select()->toArray();
        foreach ($info as &$item){
            $item['zhiqianlei_name'] = (new OptService())->get_base_showname('cai_zhiqianlei', $item['zhiqianlei']);
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

            $item['yishoupiao'] = QianZhiGuanModel::alias('guan')
                ->field('list.*')
                ->join('qian_zhi zhi','zhi.id=guan.zhiid')
                ->join('qian_piao piao','piao.id=guan.piaoid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.zhilistid'=>$item['listid']])
                ->sum('difee');

            if($item['yishoupiao'] <= 0){
                $item['statename'] = '未收票';
            }else{
                if($item['yishoupiao'] < $item['listmoney']){
                    $item['statename'] = '部分收票';
                }else{
                    $item['statename'] = '已收票';
                }
            }
            $item['daishoufee'] = round($item['listmoney'] - $item['yishoupiao'], 2);


            if(isset($param['statename']) && !empty($param['statename'])){
                if($param['statename'] == $item['statename']){
                    $list[] = $item;
                }
            }else{
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function detail_qian_zhi222(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['zhi.bidid','=', $param['bidid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|b.name|b.gofuer|list.explain|list.fuer|b.number|zhi.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['list_explain']) && !empty($param['list_explain'])){
            $where[] = ['list.explain','like', '%'.$param['list_explain'].'%'];
        }
        if(isset($param['list_fuer']) && !empty($param['list_fuer'])){
            $where[] = ['list.fuer','like', '%'.$param['list_fuer'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['zhi.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['bid_number']) && !empty($param['bid_number'])){
            $where[] = ['b.number','like', '%'.$param['bid_number'].'%'];
        }
        if(isset($param['bid_state']) && !empty($param['bid_state'])){
            $where[] = ['b.state','=', $param['bid_state']];
        }
        if(isset($param['bid_gofuer']) && !empty($param['bid_gofuer'])){
            $where[] = ['b.gofuer','like', '%'.$param['bid_gofuer'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['zhi.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['zhi.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['zhiqianlei']) && !empty($param['zhiqianlei'])){
            $where[] = ['list.zhiqianlei','=', $param['zhiqianlei']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }

        $data['zhiqianlei'] = (new OptService())->get_base_info('cai_zhiqianlei');
        $data['is_zhiqianlei'] = isset($param['zhiqianlei']) ? $param['zhiqianlei'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['bid_state'] = (new BidService())->get_bid_state();
        $data['is_bid_state'] = isset($param['bid_state']) ? $param['bid_state'] : '';

        $data['data'] = QianZhiListModel::alias('list')
            ->field('zhi.*,b.name as bidname,b.number as bidnumber,b.gofuer,b.state,bill.sericnum,list.id as listid,
            list.zhiqianlei,list.money as listmoney,list.explain as listexplain,list.fuer,list.banknum,list.bankname,
            list.incdaotype,list.difee')
            ->join('qian_zhi zhi','zhi.id=list.zhiid')
            ->leftJoin('flow_bill bill','bill.id=zhi.billid')
            ->leftJoin('bid b','b.id=zhi.bidid')
            ->where($where)
            ->order('zhi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['zhiqianlei_name'] = (new OptService())->get_base_showname('cai_zhiqianlei', $item['zhiqianlei']);
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);

                $item['yishoupiao'] = QianZhiGuanModel::alias('guan')
                    ->field('list.*')
                    ->join('qian_zhi zhi','zhi.id=guan.zhiid')
                    ->join('qian_piao piao','piao.id=guan.piaoid')
                    ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.zhilistid'=>$item['listid']])
                    ->sum('difee');

                if($item['yishoupiao'] <= 0){
                    $item['statename'] = '未收票';
                }else{
                    if($item['yishoupiao'] < $item['listmoney']){
                        $item['statename'] = '部分收票';
                    }else{
                        $item['statename'] = '已收票';
                    }
                }
                $item['daishoufee'] = round($item['listmoney'] - $item['yishoupiao'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_qian_zhi_guan(Request $request){
        $fuer = $request->post('xiaoer', '', 'string');
        $bidid = $request->post('bidid', 0, 'int');
        if(!$fuer || !$bidid){
            return outputJson(2000,lang('code_2000'),['list'=>[]]);
        }

        $list = [];
        $zhiList = QianZhiListModel::alias('list')
            ->field('list.*,zhi.billid,b.name as bidname')
            ->join('qian_zhi zhi','zhi.id=list.zhiid')
            ->leftJoin('bid b','b.id=zhi.bidid')
            ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
            ->where(['list.fuer'=>$fuer,'zhi.bidid'=>$bidid])
            ->select()->toArray();
        foreach ($zhiList as $item){
            $item['sericnum'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$item['billid']])->value('sericnum');
            $item['status_name'] = '审批通过';

            $item['zhiid'] = $item['zhiid'];
            $item['zhilistid'] = $item['id'];
            $item['zhiqianlei_name'] = (new OptService())->get_base_showname('cai_zhiqianlei', $item['zhiqianlei']);

            $item['yishoupiao'] = QianZhiGuanModel::alias('guan')
                ->field('list.*')
                ->join('qian_zhi zhi','zhi.id=guan.zhiid')
                ->join('qian_piao piao','piao.id=guan.piaoid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.zhilistid'=>$item['id']])
                ->sum('difee');

            if($item['yishoupiao'] <= 0){
                $item['statename'] = '未收票';
            }else{
                if($item['yishoupiao'] < $item['money']){
                    $item['statename'] = '部分收票';
                }else{
                    continue;
                }
            }
            $item['daishoufee'] = round($item['money'] - $item['yishoupiao'], 2);

            $list[] = $item;
        }

        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_qian_zhi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['zhi.deleted_user','=', 0];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','in',[0,1]];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['zhi.bidid','=', $param['bidid']];
        }
        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['b.jinglei','=', $param['jinglei']];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['b.gofuer','=', $param['gofuer']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['b.name|zhi.zhai|b.number|b.gofuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['zhi.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['bid_number']) && !empty($param['bid_number'])){
            $where[] = ['b.number','like', '%'.$param['bid_number'].'%'];
        }
        if(isset($param['bid_state']) && !empty($param['bid_state'])){
            $where[] = ['b.state','=', $param['bid_state']];
        }
        if(isset($param['fulei']) && !empty($param['fulei'])){
            $where[] = ['zhi.fulei','=', $param['fulei']];
        }
        if(isset($param['bid_gofuer']) && !empty($param['bid_gofuer'])){
            $where[] = ['b.gofuer','like', '%'.$param['bid_gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['zhi.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['zhi.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }


        $data['bid_state'] = (new BidService())->get_bid_state();
        $data['is_bid_state'] = isset($param['bid_state']) ? $param['bid_state'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['fulei'] = (new OptService())->get_base_info('cai_qianzhifulei');
        $data['is_fulei'] = isset($param['fulei']) ? $param['fulei'] : '';

        $data['data'] = QianZhiModel::alias('zhi')
            ->field('zhi.*,b.name as bidname,b.number as bidnumber,b.state as bidstate,b.status as bidstatus,b.jinglei
            ,b.gofuer,gui_unit.name as gui_unit_name')
            ->join('bid b','b.id=zhi.bidid')
            ->leftJoin('dept gui_unit','gui_unit.id=b.gui_unit')
            ->where($where)
            ->where((new MenuService())->get_miss_where('qian_zhi', 'zhi'))
            ->order('zhi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['fulei_name'] = (new OptService())->get_base_showname('cai_qianzhifulei', $item['fulei']);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen(['status'=>$item['bidstatus'],'state'=>$item['bidstate']]);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('qian_zhi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_qian_zhi(Request $request){
        $param = $request->post();
        $data = QianZhiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            QianZhiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'qian_zhi', $data['id'], '前期费支付');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_qian_zhi(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = QianZhiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid'], $data['data']['id']);

        $data['data']['fulei_name'] = (new OptService())->get_base_showname('cai_qianzhifulei', $data['data']['fulei']);

        $list = QianZhiListModel::where(['zhiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['zhiqianlei_name'] = (new OptService())->get_base_showname('cai_zhiqianlei', $item['zhiqianlei']);
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
            $item['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid'], $item['id']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('qian_zhi', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function detail_qian_piao(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['piao.deleted_user','=', 0];
        $where[] = ['piao.comid','=',$this->userinfo['comid']];
        $where[] = ['piao.isturn','=',1];
        $where[] = ['piao.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|b.name|b.gofuer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|b.name|b.number|b.gofuer|piao.flowuser|list.nums|list.xiaoer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['list_nums']) && !empty($param['list_nums'])){
            $where[] = ['list.nums','like', '%'.$param['list_nums'].'%'];
        }
        if(isset($param['list_xiaoer']) && !empty($param['list_xiaoer'])){
            $where[] = ['list.xiaoer','like', '%'.$param['list_xiaoer'].'%'];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['list.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['huidt']) && !empty($param['huidt'])){
            $where[] = ['list.huidt','between', explode(' - ', $param['huidt'])];
        }
        if(isset($param['bid_number']) && !empty($param['bid_number'])){
            $where[] = ['b.number','like', '%'.$param['bid_number'].'%'];
        }
        if(isset($param['bid_state']) && !empty($param['bid_state'])){
            $where[] = ['b.state','=', $param['bid_state']];
        }
        if(isset($param['bid_gofuer']) && !empty($param['bid_gofuer'])){
            $where[] = ['b.gofuer','like', '%'.$param['bid_gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['piao.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['piao.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['qianpiaotype']) && $param['qianpiaotype'] != ''){
            $where[] = ['list.qianpiaotype','=', $param['qianpiaotype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['piao.status','=', $param['status']];
        }

        $data['bid_state'] = (new BidService())->get_bid_state();
        $data['is_bid_state'] = isset($param['bid_state']) ? $param['bid_state'] : '';

        $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
        $data['is_qianpiaotype'] = isset($param['qianpiaotype']) ? $param['qianpiaotype'] : '';

        $data['data'] = QianPiaoListModel::alias('list')
            ->field('piao.*,b.name as bidname,b.number as bidnumber,b.gofuer,b.state,bill.sericnum,list.id as listid,
            list.xiaoer,list.qianpiaotype,list.nums,list.kaidt,list.fee,list.rate,list.ratefee as listratefee,
            list.money as listmoney,list.huidt,list.explain as listexplain,list.difee')
            ->join('qian_piao piao','piao.id=list.piaoid')
            ->leftJoin('flow_bill bill','bill.id=piao.billid')
            ->leftJoin('bid b','b.id=piao.bidid')
            ->where($where)
            ->order('piao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

                $item['yifufee'] = QianZhiGuanModel::alias('guan')
                    ->field('list.*')
                    ->join('qian_zhi zhi','zhi.id=guan.zhiid')
                    ->join('qian_piao piao','piao.id=guan.piaoid')
                    ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.piaolistid'=>$item['listid']])
                    ->sum('difee');
                if($item['yifufee'] <= 0){
                    $item['statename'] = '未付款';
                }else{
                    if($item['yifufee'] < $item['listmoney']){
                        $item['statename'] = '部分付款';
                    }else{
                        $item['statename'] = '已付款';
                    }
                }
                $item['daifufee'] = round($item['money'] - $item['yifufee'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_qian_piao_guan(Request $request){
        $xiaoer = $request->post('xiaoer', '', 'string');
        if(!$xiaoer){
            return outputJson(2000,lang('code_2000'),['list'=>[]]);
        }

        $list = [];
        $qianList = QianPiaoListModel::alias('list')
            ->field('list.*')
            ->join('qian_piao qian','qian.id=list.piaoid')
            ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
            ->where(['list.xiaoer'=>$xiaoer])
            ->select()->toArray();
        foreach ($qianList as $item){
            $item['piaoid'] = $item['piaoid'];
            $item['piaolistid'] = $item['id'];

            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

            $item['yifufee'] = QianZhiGuanModel::alias('guan')
                ->field('list.*')
                ->join('qian_zhi zhi','zhi.id=guan.zhiid')
                ->join('qian_piao piao','piao.id=guan.piaoid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                ->where(['guan.piaolistid'=>$item['id']])
                ->sum('difee');
            if($item['yifufee'] <= 0){
                $item['statename'] = '未付款';
            }else{
                if($item['yifufee'] < $item['money']){
                    $item['statename'] = '部分付款';
                }else{
                    continue;
                }
            }
            $item['daifufee'] = round($item['money'] - $item['yifufee'], 2);

            $list[] = $item;
        }

        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_qian_piao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['piao.deleted_user','=', 0];
        $where[] = ['piao.comid','=',$this->userinfo['comid']];
        $where[] = ['piao.isturn','=',1];
        $where[] = ['piao.status','in',[0,1]];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['piao.bidid','=', $param['bidid']];
        }
        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['b.jinglei','=', $param['jinglei']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['b.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['b.name|b.number|b.gofuer|piao.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['bid_number']) && !empty($param['bid_number'])){
            $where[] = ['b.number','like', '%'.$param['bid_number'].'%'];
        }
        if(isset($param['bid_state']) && !empty($param['bid_state'])){
            $where[] = ['b.state','=', $param['bid_state']];
        }
        if(isset($param['bid_gofuer']) && !empty($param['bid_gofuer'])){
            $where[] = ['b.gofuer','like', '%'.$param['bid_gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['piao.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['piao.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['piao.status','=', $param['status']];
        }

        $data['bid_state'] = (new BidService())->get_bid_state();
        $data['is_bid_state'] = isset($param['bid_state']) ? $param['bid_state'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['data'] = QianPiaoModel::alias('piao')
            ->field('piao.*,b.name as bidname,b.number as bidnumber,b.state as bidstate,b.status as bidstatus,b.jinglei
            ,b.gofuer,gui_unit.name as gui_unit_name')
            ->join('bid b','b.id=piao.bidid')
            ->leftJoin('dept gui_unit','gui_unit.id=b.gui_unit')
            ->where($where)
            ->where((new MenuService())->get_miss_where('qian_piao', 'piao'))
            ->order('piao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen(['status'=>$item['bidstatus'],'state'=>$item['bidstate']]);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('qian_piao');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_qian_piao(Request $request){
        $param = $request->post();
        $data = QianPiaoModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            QianPiaoModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'qian_piao', $data['id'], '前期费进项票');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_qian_piao(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = QianPiaoModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid']);

        $list = QianPiaoListModel::where(['piaoid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('qian_piao', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_tou_jiao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jiao.deleted_user','=', 0];
        $where[] = ['jiao.comid','=',$this->userinfo['comid']];
        $where[] = ['jiao.isturn','=',1];
        $where[] = ['jiao.status','in',[0,1]];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['jiao.bidid','=', $param['bidid']];
        }
        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['b.jinglei','=', $param['jinglei']];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['b.gofuer','=', $param['gofuer']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['b.name|jiao.zhai|b.number|b.gofuer|jiao.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['jiao.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['ziqu']) && !empty($param['ziqu'])){
            $where[] = ['jiao.ziqu','=', $param['ziqu']];
        }
        if(isset($param['fandt']) && !empty($param['fandt'])){
            $where[] = ['jiao.fandt','between', explode(' - ', $param['fandt'])];
        }
        if(isset($param['bid_number']) && !empty($param['bid_number'])){
            $where[] = ['b.number','like', '%'.$param['bid_number'].'%'];
        }
        if(isset($param['bid_state']) && !empty($param['bid_state'])){
            $where[] = ['b.state','=', $param['bid_state']];
        }
        if(isset($param['bid_gofuer']) && !empty($param['bid_gofuer'])){
            $where[] = ['b.gofuer','like', '%'.$param['bid_gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['jiao.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['jiao.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jiao.status','=', $param['status']];
        }

        $data['bid_state'] = (new BidService())->get_bid_state();
        $data['is_bid_state'] = isset($param['bid_state']) ? $param['bid_state'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['ziqu'] = (new QianService())->get_tou_jiao_ziqu();
        $data['is_ziqu'] = isset($param['ziqu']) ? $param['ziqu'] : '';

        $data['data'] = TouJiaoModel::alias('jiao')
            ->field('jiao.*,b.name as bidname,b.number as bidnumber,b.state as bidstate,b.status as bidstatus,b.jinglei
            ,b.gofuer,gui_unit.name as gui_unit_name')
            ->join('bid b','b.id=jiao.bidid')
            ->leftJoin('dept gui_unit','gui_unit.id=b.gui_unit')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tou_jiao', 'jiao'))
            ->order('jiao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen(['status'=>$item['bidstatus'],'state'=>$item['bidstate']]);

                $item['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $item['zhiqilei']);
                $item['ziqu_name'] = (new QianService())->get_tou_jiao_ziqu_name($item['ziqu']);

                $item['shouid_name'] = '';
                $shouidInfo = QianShouModel::where(['id'=>$item['shouid']])->find();
                if($shouidInfo){
                    $item['shouid_name'] = $shouidInfo['zhai'].$shouidInfo['money'];
                }

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('tou_jiao');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_tou_jiao(Request $request){
        $param = $request->post();
        $data = TouJiaoModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            TouJiaoModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'tou_jiao', $data['id'], '投标保证金缴纳');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_tou_jiao(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = TouJiaoModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid']);
        $data['shouidinfo'] = QianShouModel::where(['id'=>$data['data']['shouid']])->find();
        $list = TouJiaoListModel::where(['jiaoid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
            $item['kuaninfo'] = (new FinanceService())->get_acckuan_info_bybillid($data['data']['billid'], $item['id']);
        }
        $data['list'] = $list;


        $data['data']['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $data['data']['zhiqilei']);
        $data['data']['ziqu_name'] = (new QianService())->get_tou_jiao_ziqu_name($data['data']['ziqu']);

        $data['files'] = (new OptService())->get_file('tou_jiao', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_tou_fan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['fan.deleted_user','=', 0];
        $where[] = ['fan.comid','=',$this->userinfo['comid']];
        $where[] = ['fan.isturn','=',1];
        $where[] = ['fan.status','in',[0,1]];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['fan.bidid','=', $param['bidid']];
        }
        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['b.jinglei','=', $param['jinglei']];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['b.gofuer','=', $param['gofuer']];
        }
        if(isset($param['ziqu']) && !empty($param['ziqu'])){
            $where[] = ['jiao.ziqu','=', $param['ziqu']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['b.name|fan.zhai|b.number|b.gofuer|fan.flowuser|fan.shouer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['fan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['fan.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['fan.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['bid_number']) && !empty($param['bid_number'])){
            $where[] = ['b.number','like', '%'.$param['bid_number'].'%'];
        }
        if(isset($param['bid_state']) && !empty($param['bid_state'])){
            $where[] = ['b.state','=', $param['bid_state']];
        }
        if(isset($param['bid_gofuer']) && !empty($param['bid_gofuer'])){
            $where[] = ['b.gofuer','like', '%'.$param['bid_gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['fan.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['fan.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['fan.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['fan.status','=', $param['status']];
        }

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['bid_state'] = (new BidService())->get_bid_state();
        $data['is_bid_state'] = isset($param['bid_state']) ? $param['bid_state'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['data'] = TouFanModel::alias('fan')
            ->field('fan.*,b.name as bidname,b.number as bidnumber,b.state as bidstate,b.status as bidstatus,b.jinglei
            ,b.gofuer,gui_unit.name as gui_unit_name,jiao.money as jiaomoney,acc.name as accname,acc.accnum')
            ->join('bid b','b.id=fan.bidid')
            ->leftJoin('dept gui_unit','gui_unit.id=b.gui_unit')
            ->leftJoin('account acc','acc.id=fan.accid')
            ->leftJoin('tou_jiao jiao','jiao.id=fan.jiaoid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tou_fan', 'fan'))
            ->order('fan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen(['status'=>$item['bidstatus'],'state'=>$item['bidstate']]);

                $item['qishoulei_name'] = (new OptService())->get_base_showname('cai_qishoulei', $item['qishoulei']);
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accid_name'] = $item['accname'].'('.$item['accnum'].')';
                $item['jiaosericnum'] = FlowBillModel::where(['modenum'=>'tou_jiao','mid'=>$item['jiaoid']])->value('sericnum');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('tou_fan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_tou_fan(Request $request){
        $param = $request->post();
        $data = TouFanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            TouFanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'tou_fan', $data['id'], '投标保证金返还');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_tou_fan(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = TouFanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid']);
        $data['jiaoidinfo'] = TouJiaoModel::where(['id'=>$data['data']['jiaoid']])->find();
        if($data['jiaoidinfo']){
            $data['jiaoidinfo']['jiaosericnum'] = FlowBillModel::where(['modenum'=>'tou_jiao','mid'=>$data['jiaoidinfo']['id']])->value('sericnum');
        }
        $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

        $data['data']['qishoulei_name'] = (new OptService())->get_base_showname('cai_qishoulei', $data['data']['qishoulei']);
        $data['data']['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $data['data']['incdaotype']);

        $data['files'] = (new OptService())->get_file('tou_fan', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_tou_han(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['han.deleted_user','=', 0];
        $where[] = ['han.comid','=',$this->userinfo['comid']];
        $where[] = ['han.isturn','=',1];
        $where[] = ['han.status','in',[0,1]];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['han.bidid','=', $param['bidid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['b.name|han.name|b.number|b.gofuer|han.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['han.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['han.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['daodt']) && !empty($param['daodt'])){
            $where[] = ['han.daodt','between', explode(' - ', $param['daodt'])];
        }
        if(isset($param['bid_number']) && !empty($param['bid_number'])){
            $where[] = ['b.number','like', '%'.$param['bid_number'].'%'];
        }
        if(isset($param['bid_state']) && !empty($param['bid_state'])){
            $where[] = ['b.state','=', $param['bid_state']];
        }
        if(isset($param['bid_gofuer']) && !empty($param['bid_gofuer'])){
            $where[] = ['b.gofuer','like', '%'.$param['bid_gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['han.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['han.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['baotai']) && !empty($param['baotai'])){
            $where[] = ['han.baotai','=', $param['baotai']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['han.status','=', $param['status']];
        }

        $data['baotai'] = (new OptService())->get_base_info('cai_baotai');
        $data['is_baotai'] = isset($param['baotai']) ? $param['baotai'] : '';

        $data['bid_state'] = (new BidService())->get_bid_state();
        $data['is_bid_state'] = isset($param['bid_state']) ? $param['bid_state'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['data'] = TouHanModel::alias('han')
            ->field('han.*,b.name as bidname,b.number as bidnumber,b.state as bidstate,b.status as bidstatus
            ,b.gofuer,gui_unit.name as gui_unit_name')
            ->join('bid b','b.id=han.bidid')
            ->leftJoin('dept gui_unit','gui_unit.id=b.gui_unit')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tou_han', 'han'))
            ->order('han.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen(['status'=>$item['bidstatus'],'state'=>$item['bidstate']]);

                $item['baotai_name'] = (new OptService())->get_base_showname('cai_baotai', $item['baotai']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('tou_han');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_tou_han(Request $request){
        $param = $request->post();
        $data = TouHanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            TouHanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'tou_han', $data['id'], '投标保函');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_tou_han(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = TouHanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid']);

        $data['data']['baotai_name'] = (new OptService())->get_base_showname('cai_baotai', $data['data']['baotai']);

        $data['files'] = (new OptService())->get_file('tou_han', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

















}
